package ee.serge.client.content.cp;

import java.util.List;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Label;

import ee.serge.client.AlertBox;
import ee.serge.client.Move;
import ee.serge.service.Service;
import ee.serge.shared.Feedback;
import ee.serge.shared.IPFeedbackData;
import ee.serge.util.AdminContent;
import ee.serge.util.BTextBox;
import ee.serge.util.ClickLabel;
import ee.serge.util.Content;
import ee.serge.util.FormPanel;
import ee.serge.util.Loader;
import ee.serge.util.RatingPanel;
import ee.serge.util.SimpleCallback;
import ee.serge.util.URLData;
import ee.serge.util.bootstrap.BButton;
import ee.serge.util.bootstrap.BButtonBar;
import ee.serge.util.bootstrap.BTable;

public class IPContent extends AdminContent {

   private static final String NAME = "cp/ip_view";
   
   public static String getName() {
      return NAME;
   }
   
   public static URLData getURLData(String ip) {
      URLData data = new URLData();
      data.setName(NAME);
      data.add("ip", ip);
      return data;
   }
   
   public static URLData getURLData() {
      URLData data = new URLData();
      data.setName(NAME);
      return data;
   }
   
   private BTextBox ipTextBox;
   private static String ip;
   
   public static void show() {
      ip = Move.get().getCurrentURLData().get("ip");
      if (ip != null && ip.length() > 0) {
         Service.getControlPanelService().getFeedbackListWithIP(ip, new SimpleCallback<IPFeedbackData>() {
            @Override
            public void success(IPFeedbackData result) {
               Move.get().setContent(new IPContent(result));
            }
         });
      } else {
         Move.get().setContent(new IPContent());
      }
      Loader.hide();
   }
   
   public IPContent(IPFeedbackData result) {
      buildViewPanel(result);
   }
      
   public IPContent() {
      buildSearchPanel();
   }
   
   public void buildSearchPanel() {
      FormPanel form = new FormPanel();
      form.add("IP address", ipTextBox = new BTextBox());
      add(form);
      
      BButtonBar buttonBar = new BButtonBar();
      buttonBar.add(new BButton("Search", BButton.PRIMARY, new SearchHandler()));
      add(buttonBar);
   }
   
   public void buildViewPanel(IPFeedbackData data) {
      ip = data.getIp();
      String header = ip;
      if (data.isBanned()) {
         header += " (banned)";
      }
      setHeader(header);
      String[] headers = {"#", "Rating", "Review", ""};
      List<Feedback> feedbackList = data.getFeedbackList();
      BTable table = new BTable(feedbackList.size(), headers);
      for (int i = 0; i < feedbackList.size(); i++) {
         Feedback feedback = feedbackList.get(i);
         table.setText(i, 0, i + 1);
         table.setWidget(i, 1, new RatingPanel(feedback.getRating(), false));
         table.setText(i, 2, feedback.getComment());
         table.setWidget(i, 3, new ClickLabel("Remove", new RemoveFeedbackHandler(feedback.getKey())));
      }
      add(table);
      
      BButtonBar buttonBar = new BButtonBar();
      if (data.isBanned()) {
         buttonBar.add(new BButton("Remove ban", BButton.PRIMARY, new RemoveBanHandler()));
      } else {
         buttonBar.add(new BButton("Ban IP", BButton.DANGER, new BanHandler()));
      }
      buttonBar.add(new BButton("Back to search", new ClickHandler() {
               
               @Override
               public void onClick(ClickEvent event) {
                  Move.get().goToURLData(getURLData());
               }
            }));
      add(buttonBar);
   }
   
   private class RemoveFeedbackHandler implements ClickHandler {
      private String key;
      public RemoveFeedbackHandler(String key) {
         this.key = key;
      }
      @Override
      public void onClick(ClickEvent event) {
         Service.getControlPanelService().removeFeedback(key, new SimpleCallback<Void>() {

            @Override
            public void success(Void result) {
               AlertBox.showSuccess("Review removed");
            }
         });
      }
   }
   
   private class BanHandler implements ClickHandler {

      @Override
      public void onClick(ClickEvent event) {
         Service.getControlPanelService().banIP(ip, new SimpleCallback<Void>() {

            @Override
            public void success(Void result) {
               Move.get().refresh();
               AlertBox.showSuccess("IP banned");
            }

         });
      }
      
   }
   
   private class RemoveBanHandler implements ClickHandler {

      @Override
      public void onClick(ClickEvent event) {
         Service.getControlPanelService().removeBan(ip, new SimpleCallback<Void>() {

            @Override
            public void success(Void result) {
               Move.get().refresh();
               AlertBox.showSuccess("Ban removed");
            }

         });
      }
      
   }
   
   private class SearchHandler implements ClickHandler {

      @Override
      public void onClick(ClickEvent event) {
         Move.get().goToURLData(getURLData(ipTextBox.getText()));
         Loader.show();
      }
      
   }
   
}
